package com.wf.smartcomm.server.admin.action;

import java.io.IOException;
import java.math.BigDecimal;

import javax.annotation.Resource;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.context.annotation.Scope;
import org.quartz.impl.StdScheduler;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Discount;
import com.wf.smartcomm.server.bean.PropertyFee;
import com.wf.smartcomm.server.job.QuartzJobFactory;
import com.wf.smartcomm.server.job.ScheduleJob;
import com.wf.smartcomm.server.service.DiscountService;
import com.wf.smartcomm.server.service.FeeService;
import com.wf.smartcomm.server.service.ScheduleService;
import com.wf.smartcomm.server.util.ConstantUtils;

@Controller
@Scope("prototype")
public class FeeAction extends BaseAction implements ModelDriven<Discount>{

	private Discount discount = new Discount();
	
	@Resource
	private FeeService feeServiceImpl;
	
	@Resource
	private DiscountService discountServiceImpl;
	
	public String setting(){
		session.setAttribute("fee", feeServiceImpl.get(currentUser.getLoginVillage()));
		session.setAttribute("discount", discountServiceImpl.get(currentUser.getLoginVillage()));
		return "success";
	}
	
	public void save() throws IOException, SchedulerException{
		PropertyFee fee = feeServiceImpl.get(currentUser.getLoginVillage());
		if (fee != null) {
			fee.setPrice(new BigDecimal(request.getParameter("price")));
			fee.setPeriod(Integer.parseInt(request.getParameter("period")));
			feeServiceImpl.update(fee);
		}else {
			fee = new PropertyFee();
			fee.setPeriod(Integer.parseInt(request.getParameter("period")));
			fee.setPrice(new BigDecimal(request.getParameter("price")));
			fee.setVillage(currentUser.getLoginVillage());
			fee.setUser(currentUser);
			feeServiceImpl.add(fee);
		}
		
		Discount old = discountServiceImpl.get(currentUser.getLoginVillage());
		
		if (old != null) {
			if (discount.getDiscount() != null) {
				old.setUser(currentUser);
				old.setDiscount(discount.getDiscount());
				old.setBeginAt(discount.getBeginAt());
				old.setEndAt(discount.getEndAt());
				old.setFromAt(discount.getFromAt());
				old.setToAt(discount.getToAt());
				discountServiceImpl.update(discount);
			}
		}else {
			if (discount.getDiscount() != null) {
				discountServiceImpl.add(discount);
			}
		}
		
		printJsonData(200);
	}
	
	@Override
	public Discount getModel() {
		return discount;
	}
}
